<template>
  <div class="qrcode-detail-container">
    <!-- 顶部导航 -->
    <div class="detail-header">
      <a-button type="text" @click="goBack" class="back-btn">
        <template #icon>
          <ArrowLeftOutlined />
        </template>
        返回
      </a-button>
      <div class="header-content">
        <div class="qr-info">
          <QrcodeOutlined class="qr-icon" />
          <div class="qr-meta">
            <h1 class="qr-title">{{ qrDetail?.qrName }}</h1>
            <div class="qr-subtitle">
              <span class="qr-code">{{ qrDetail?.qrCode }}</span>
                             <a-tag :color="getQrStatusColor(qrDetail?.qrStatus || 0)" class="status-tag">
                 {{ getQrStatusName(qrDetail?.qrStatus || 0) }}
              </a-tag>
            </div>
          </div>
        </div>
        <div class="header-actions">
          <a-button type="primary" @click="editQrCode">
            <template #icon>
              <EditOutlined />
            </template>
            编辑
          </a-button>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="detail-content">
      <a-row :gutter="24">
        <!-- 左侧：基本信息 -->
        <a-col :span="16">
          <a-card title="基本信息" class="info-card">
            <template #extra>
              <CopyOutlined @click="copyQrCode" class="copy-icon" title="复制聚合码牌代码" />
            </template>

            <a-descriptions :column="2" size="middle" bordered>
              <a-descriptions-item label="聚合码牌名称" :span="2">
                <span class="important-text">{{ qrDetail?.qrName }}</span>
              </a-descriptions-item>

              <a-descriptions-item label="聚合码牌代码">
                <code class="qr-code-display">{{ qrDetail?.qrCode }}</code>
              </a-descriptions-item>

                             <a-descriptions-item label="状态">
                 <a-tag :color="getQrStatusColor(qrDetail?.qrStatus || 0)" size="large">
                   {{ getQrStatusName(qrDetail?.qrStatus || 0) }}
                </a-tag>
              </a-descriptions-item>

              <a-descriptions-item label="所属商户" :span="2">
                <div class="merchant-info">
                  <UserOutlined class="merchant-icon" />
                  <span class="merchant-name">{{ qrDetail?.merchantName }}</span>
                  <span class="merchant-id">( {{ qrDetail?.merchantId }} )</span>
                </div>
              </a-descriptions-item>

              <a-descriptions-item label="绑定APP" :span="2">
                <div v-if="qrDetail?.boundAppId" class="app-info">
                  <MobileOutlined class="app-icon" />
                  <span class="app-name">{{ getAppName(qrDetail.boundAppId) }}</span>
                  <span class="app-id">( {{ qrDetail?.boundAppId }} )</span>
                  <a-tag color="green" size="small">已绑定</a-tag>
                </div>
                <span v-else class="no-binding">
                  <DisconnectOutlined style="color: #ccc; margin-right: 6px;" />
                  未绑定应用
                </span>
              </a-descriptions-item>

              <a-descriptions-item label="有效期">
                <ClockCircleOutlined style="margin-right: 6px;" />
                {{ qrDetail?.validPeriod }}
              </a-descriptions-item>

              <a-descriptions-item label="绑定时间">
                <span v-if="qrDetail?.bindTime">
                  {{ formatDateTime(qrDetail.bindTime) }}
                </span>
                <span v-else class="no-data">未绑定</span>
              </a-descriptions-item>
            </a-descriptions>
          </a-card>

          <!-- 技术信息 -->
          <a-card title="技术信息" class="info-card" style="margin-top: 16px;">
            <a-descriptions :column="1" size="middle" bordered>
              <a-descriptions-item label="聚合码牌URL">
                <div class="url-display">
                  <code>{{ qrDetail?.qrUrl }}</code>
                  <a-button type="text" size="small" @click="copyUrl" class="copy-btn">
                    <CopyOutlined />
                  </a-button>
                </div>
              </a-descriptions-item>

              <a-descriptions-item label="聚合码牌内容">
                <div class="content-display">
                  <pre class="qr-content">{{ qrDetail?.qrContent }}</pre>
                  <a-button type="text" size="small" @click="copyContent" class="copy-btn">
                    <CopyOutlined />
                  </a-button>
                </div>
              </a-descriptions-item>
            </a-descriptions>
          </a-card>
        </a-col>

        <!-- 右侧：聚合码牌预览 -->
        <a-col :span="8">
          <a-card title="聚合码牌预览" class="preview-card">
            <div class="qr-preview">
              <div class="qr-visual">
                <QrcodeOutlined class="qr-large-icon" />
                <div class="qr-scanner-line"></div>
              </div>
              <div class="qr-actions">
                <a-button type="primary" block @click="downloadQr" style="margin-bottom: 8px;">
                  <DownloadOutlined />
                  下载聚合码牌
                </a-button>
                <a-button block @click="previewQr">
                  <EyeOutlined />
                  预览聚合码牌
                </a-button>
              </div>
            </div>
          </a-card>

          <!-- 操作记录 -->
          <a-card title="操作记录" class="info-card" style="margin-top: 16px;">
            <a-timeline>
              <a-timeline-item color="green">
                <template #dot>
                  <CheckCircleOutlined style="color: #52c41a;" />
                </template>
                <div class="timeline-content">
                  <div class="timeline-title">聚合码牌创建</div>
                  <div class="timeline-time">{{ formatDateTime(qrDetail?.createdTime) }}</div>
                  <div class="timeline-user">创建者：{{ qrDetail?.createdBy }}</div>
                </div>
              </a-timeline-item>

              <a-timeline-item v-if="qrDetail?.bindTime" color="blue">
                <template #dot>
                  <LinkOutlined style="color: #1890ff;" />
                </template>
                <div class="timeline-content">
                  <div class="timeline-title">绑定应用</div>
                  <div class="timeline-time">{{ formatDateTime(qrDetail.bindTime) }}</div>
                  <div class="timeline-desc">绑定到：{{ getAppName(qrDetail.boundAppId) }}</div>
                </div>
              </a-timeline-item>

              <a-timeline-item>
                <template #dot>
                  <EditOutlined style="color: #faad14;" />
                </template>
                <div class="timeline-content">
                  <div class="timeline-title">最后更新</div>
                  <div class="timeline-time">{{ formatDateTime(qrDetail?.updatedTime) }}</div>
                </div>
              </a-timeline-item>
            </a-timeline>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import {
  ArrowLeftOutlined,
  QrcodeOutlined,
  EditOutlined,
  CopyOutlined,
  UserOutlined,
  MobileOutlined,
  DisconnectOutlined,
  ClockCircleOutlined,
  DownloadOutlined,
  EyeOutlined,
  CheckCircleOutlined,
  LinkOutlined
} from '@ant-design/icons-vue'
import {
  qrCodes,
  getQrStatusName,
  getQrStatusColor,
  type QRCode
} from '@/mock'
import { appConfigs } from '@/mock/appconfigs'

const router = useRouter()
const route = useRoute()

const qrDetail = ref<QRCode | null>(null)

// 获取聚合码牌详情
const loadQrDetail = () => {
  const qrId = route.params.id as string
  const qr = qrCodes.find(q => q.qrId === qrId)
  if (qr) {
    qrDetail.value = qr
  } else {
    message.error('聚合码牌不存在')
    goBack()
  }
}

// 返回列表
const goBack = () => {
  router.back()
}

// 编辑聚合码牌
const editQrCode = () => {
  router.push(`/development-test/qrcode-edit/${qrDetail.value?.qrId}`)
}

// 复制聚合码牌代码
const copyQrCode = () => {
  if (qrDetail.value?.qrCode) {
    navigator.clipboard.writeText(qrDetail.value.qrCode)
    message.success('聚合码牌代码已复制')
  }
}

// 根据应用ID获取应用名称
const getAppName = (appId: string) => {
  const app = appConfigs.find(app => app.appId === appId)
  return app ? app.appName : appId
}

// 复制URL
const copyUrl = () => {
  if (qrDetail.value?.qrUrl) {
    navigator.clipboard.writeText(qrDetail.value.qrUrl)
    message.success('聚合码牌URL已复制')
  }
}

// 复制内容
const copyContent = () => {
  if (qrDetail.value?.qrContent) {
    navigator.clipboard.writeText(qrDetail.value.qrContent)
    message.success('聚合码牌内容已复制')
  }
}

// 下载聚合码牌
const downloadQr = () => {
  message.success('聚合码牌下载功能开发中...')
}

// 预览聚合码牌
const previewQr = () => {
  message.info('聚合码牌预览功能开发中...')
}

// 格式化日期时间
const formatDateTime = (dateTime?: string) => {
  if (!dateTime) return '暂无'
  return new Date(dateTime).toLocaleString('zh-CN')
}

onMounted(() => {
  loadQrDetail()
})
</script>

<style scoped>
.qrcode-detail-container {
  min-height: 100vh;
  background: #f5f5f5;
}

.detail-header {
  background: #fff;
  padding: 20px 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border-bottom: 1px solid #f0f0f0;
}

.back-btn {
  padding: 4px 8px;
  margin-right: 16px;
  color: #666;
  font-size: 14px;
}

.back-btn:hover {
  color: #1890ff;
  background: #f0f8ff;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.qr-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.qr-icon {
  font-size: 48px;
  color: #1890ff;
  background: #f0f8ff;
  padding: 16px;
  border-radius: 12px;
}

.qr-title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.qr-subtitle {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}

.qr-code {
  font-family: monospace;
  font-size: 14px;
  color: #666;
  background: #f5f5f5;
  padding: 4px 8px;
  border-radius: 4px;
}

.status-tag {
  font-weight: 500;
}

.detail-content {
  padding: 24px;
}

.info-card {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border-radius: 8px;
}

.info-card .ant-card-head {
  border-bottom: 1px solid #f0f0f0;
}

.info-card .ant-card-head-title {
  font-weight: 600;
  color: #333;
}

.copy-icon {
  color: #666;
  cursor: pointer;
  font-size: 16px;
}

.copy-icon:hover {
  color: #1890ff;
}

.important-text {
  font-weight: 600;
  color: #333;
}

.qr-code-display {
  font-family: monospace;
  font-size: 14px;
  background: #f5f5f5;
  padding: 4px 8px;
  border-radius: 4px;
  color: #d4380d;
}

.merchant-info,
.app-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.merchant-icon,
.app-icon {
  color: #1890ff;
}

.merchant-name,
.app-name {
  font-weight: 500;
  color: #333;
}

.merchant-id,
.app-id {
  color: #666;
  font-size: 12px;
}

.no-binding,
.no-data {
  color: #999;
  font-style: italic;
}

.url-display,
.content-display {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.url-display code {
  flex: 1;
  background: #f5f5f5;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 12px;
  word-break: break-all;
  color: #1890ff;
}

.qr-content {
  flex: 1;
  background: #f5f5f5;
  padding: 12px;
  border-radius: 4px;
  font-size: 12px;
  margin: 0;
  word-break: break-all;
  color: #52c41a;
  border: 1px solid #e8e8e8;
}

.copy-btn {
  padding: 4px 8px;
  color: #666;
}

.copy-btn:hover {
  color: #1890ff;
  background: #f0f8ff;
}

.preview-card {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border-radius: 8px;
}

.qr-preview {
  text-align: center;
}

.qr-visual {
  position: relative;
  padding: 40px 20px;
  background: #fafafa;
  border: 2px dashed #d9d9d9;
  border-radius: 8px;
  margin-bottom: 20px;
  overflow: hidden;
}

.qr-large-icon {
  font-size: 120px;
  color: #1890ff;
  opacity: 0.8;
}

.qr-scanner-line {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, #52c41a, transparent);
  animation: scan 2s linear infinite;
}

@keyframes scan {
  0% {
    transform: translateY(-2px);
  }
  100% {
    transform: translateY(200px);
  }
}

.timeline-content {
  padding-left: 8px;
}

.timeline-title {
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
}

.timeline-time {
  font-size: 12px;
  color: #999;
  margin-bottom: 2px;
}

.timeline-user,
.timeline-desc {
  font-size: 12px;
  color: #666;
}

.ant-descriptions-item-label {
  width: 120px;
  font-weight: 500;
  color: #333;
  background: #fafafa;
}

.ant-descriptions-item-content {
  color: #333;
}

.ant-timeline-item-content {
  margin-left: 20px;
}

@media (max-width: 768px) {
  .detail-header {
    padding: 16px;
  }

  .header-content {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }

  .qr-info {
    gap: 12px;
  }

  .qr-icon {
    font-size: 36px;
    padding: 12px;
  }

  .qr-title {
    font-size: 20px;
  }

  .detail-content {
    padding: 16px;
  }

  .ant-col {
    margin-bottom: 16px;
  }
}
</style>